<?php

class MessagesController extends AppController {

    var $name = 'Messages';
    var $uses = array('MessageTrack', 'Message', 'User', 'Group');
    var $helpers = array('Html', 'Form', 'Javascript', 'Fck');
    var $components = array('Email');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('dispute_message', 'totalnewmessage');
    }

    function index() {
        $this->layout = 'innerhomeemail';
        $msginfo = $this->User->find('first', array('conditions' =>
            array('User.id' => $this->Auth->User('id')), 'fields' => array('User.msg_archive', 'User.msg_delete'), 'recursive' => 0));
        if ($msginfo['User']['msg_archive'] == 90) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-90 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-90 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->id = $msgvalue['MessageTrack']['id'];
                $this->MessageTrack->saveField('msg_type', 2);
            }
        } elseif ($msginfo['User']['msg_archive'] == 1) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-365 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-365 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->id = $msgvalue['MessageTrack']['id'];
                $this->MessageTrack->saveField('msg_type', 2);
            }
        }
        if ($msginfo['User']['msg_delete'] == 90) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-90 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-90 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->delete($msgvalue['MessageTrack']['id']);
            }
        } elseif ($msginfo['User']['msg_delete'] == 1) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-365 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-365 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->delete($msgvalue['MessageTrack']['id']);
            }
        }
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function totalnewmessage() {
        $this->layout = NULL;
        $this->autoRender = false;
        return $this->MessageTrack->find('count', array('conditions' => array('MessageTrack.read' => 0, 'MessageTrack.to_id' => $this->Auth->User('id'), 'MessageTrack.msg_type' => 1)));
    }

    function view($id = null) {
        $this->layout = FALSE;
        if (!$id) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        $msgid = $this->MessageTrack->findById($id);
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('read', 1);
        $this->set('message', $msgid);
    }

    function view_sent($id = null) {
        $this->layout = FALSE;
        if (!$id) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        $msgid = $this->MessageTrack->findById($id);
        $this->set('message', $msgid);
    }

    function view_archive($id = null) {
        $this->layout = FALSE;
        if (!$id) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        $msgid = $this->MessageTrack->findById($id);
        $this->set('message', $msgid);
    }

    function unread($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('read', 0);
        echo 'Success!';
    }

    function read($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('read', 1);
        echo 'Success!';
    }

    function star_unstar($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $msgstate = $this->MessageTrack->read('star', $id);
        $pvalue = $msgstate['MessageTrack']['star'];
        if ($pvalue == 0) {
            $pvalue = 1;
            echo 'Mark as star successfully.';
        } else {

            $pvalue = 0;
            echo 'Mark as unstar successfully.';
        }
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('star', $pvalue);
    }

    function star($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('star', 1);
        echo 'Success!';
    }

    function unstar($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('star', 0);
        echo 'Success!';
    }

    function move_archieve($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('msg_type', 2);
        echo 'Success!';
    }

    function move_inbox($id=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $chk = $this->MessageTrack->read(null, $id);
        $this->MessageTrack->id = $id;
        if ($this->Auth->User('id') == $chk['MessageTrack']['to_id']) {
            $this->MessageTrack->saveField('msg_type', 1);
        } else {
            $this->MessageTrack->saveField('msg_type', 0);
        }
        echo 'Success!';
    }

    function setting_archieve($mval=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->User->id = $this->Auth->User('id');
        $this->User->saveField('msg_archive', $mval);
    }

    function setting_delete($mval=null) {
        $this->layout = FALSE;
        $this->autoRender = false;
        $this->User->id = $this->Auth->User('id');
        $this->User->saveField('msg_delete', $mval);
    }

    function backstage_setting() {
        $this->layout = 'admin';
        $users = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->User('id')), 'fields' => array('User.msg_archive', 'User.msg_delete'), 'recursive' => 0));
        $this->set('users', $users);
    }

    function backstage_msg_search($serchmsg=null, $type=null) {
        $this->layout = 'admin';
        $this->MessageTrack->recursive = 0;
        if (isset($_POST['serchmsg'])) {
            $serchmsg = $_POST['serchmsg'];
        }
        $this->set('serchmsg', $serchmsg);

        if (isset($this->data['type'])) {
            $type = $this->data['type'];
        }
        $this->set('type', $type);
        $userid = $this->Auth->User('id');
        if ($type == 'inbox') {
            $this->paginate = array(
                'MessageTrack' => array('limit' => 1, 'conditions' => array('OR' => array(array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1, 'Message.subject like' => '%' . $serchmsg . '%'), array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1, 'Message.message like' => '%' . $serchmsg . '%'))), 'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('backstage_index');
        } elseif ($type == 'sent') {

            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array('OR' => array(array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0, 'Message.subject like' => '%' . $serchmsg . '%'), array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0, 'Message.message like' => '%' . $serchmsg . '%'))), 'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('backstage_sent');
        } elseif ($type == 'archive') {
            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array(
                        'OR' => array(
                            array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.message like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.message like' => '%' . $serchmsg . '%')
                    )),
                    'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('backstage_archive');
        } else {

            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array('OR' => array(
                            array('MessageTrack.from_id' => $userid, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'Message.message like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.from_id' => $userid, 'Message.message like' => '%' . $serchmsg . '%'))),
                    'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
        }
    }

    function backstage_new_message($cid=null) {
        $this->layout = 'admin';
        if (!empty($this->data)) {

            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $mid = $this->Message->getLastInsertID();
                if (!empty($this->data['MessageTrack']['user_id'])) {
                    /////////for sent item//////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = $this->data['MessageTrack']['user_id'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 0;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    ///////////for inbox///////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = $this->data['MessageTrack']['user_id'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 1;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    /////////////for send email ///////////
                    /* $this->Email->smtpOptions = array(
                      'port'=>'465',
                      'timeout'=>'30',
                      'host' => 'ssl://smtp.gmail.com',
                      'username'=>'kodegeek10@gmail.com',
                      'password'=>'123456aa',
                      );
                      $this->Email->delivery = 'smtp';
                      $this->User->recursive = -1;
                      $to_email = $this->User->read('email', $this->data['MessageTrack']['user_id']);
                      $from_email = $this->User->read('email', $this->Auth->User('id'));
                      $this->Email->to = $to_email['User']['email'];
                      $this->Email->subject = $this->data['Message']['subject'];
                      //$this->Email->replyTo = "<noreply@trakdat.com>";
                      $this->Email->from = $from_email['User']['email'];
                      $this->Email->sendAs = 'html';
                      $this->Email->template = "newmessage";
                      $this->set('mailbody',$this->data['Message']['message']);
                      //$this->set('server_name', $_SERVER['SERVER_NAME']);
                      $this->Email->send();
                      $this->Email->reset(); */
                }

                if (!empty($this->data['to_id'])) {
                    $toid = trim($this->data['to_id']);
                    $this->User->recursive = -1;
                    $uinfo = $this->User->findByUsername($toid);
                    if (!empty($uinfo)) {
                        $toid = $uinfo['User']['id'];
                        if ($toid != $this->data['MessageTrack']['user_id']) {
                            /////////for sent item//////
                            $this->data['MessageTrack']['id'] = null;
                            $this->data['MessageTrack']['message_id'] = $mid;
                            $this->data['MessageTrack']['to_id'] = $toid;
                            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                            $this->data['MessageTrack']['msg_type'] = 0;
                            $this->data['MessageTrack']['read'] = 0;
                            $this->data['MessageTrack']['star'] = 0;
                            $this->data['MessageTrack']['reply'] = $this->data['reply'];
                            $this->MessageTrack->save($this->data);
                            ///////////for inbox///////
                            $this->data['MessageTrack']['id'] = null;
                            $this->data['MessageTrack']['message_id'] = $mid;
                            $this->data['MessageTrack']['to_id'] = $toid;
                            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                            $this->data['MessageTrack']['msg_type'] = 1;
                            $this->data['MessageTrack']['read'] = 0;
                            $this->data['MessageTrack']['star'] = 0;
                            $this->data['MessageTrack']['reply'] = $this->data['reply'];
                            $this->MessageTrack->save($this->data);
                            ////////////send mail//////////
                            /* $this->Email->smtpOptions = array(
                              'port'=>'465',
                              'timeout'=>'30',
                              'host' => 'ssl://smtp.gmail.com',
                              'username'=>'kodegeek10@gmail.com',
                              'password'=>'123456aa',
                              );
                              $this->Email->delivery = 'smtp';
                              $this->User->recursive = -1;
                              $to_email = $this->User->read('email', $toid);
                              $from_email = $this->User->read('email', $this->Auth->User('id'));
                              $this->Email->to = $to_email['User']['email'];
                              $this->Email->subject = $this->data['Message']['subject'];
                              //$this->Email->replyTo = "<noreply@trakdat.com>";
                              $this->Email->from = $from_email['User']['email'];
                              $this->Email->sendAs = 'html';
                              $this->Email->template = "newmessage";
                              $this->set('mailbody',$this->data['Message']['message']);
                              //$this->set('server_name', $_SERVER['SERVER_NAME']);
                              $this->Email->send();
                             */
                        }
                    }
                }
                $this->Session->setFlash(__('The message has been sent successfully.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }

        $users = $this->Group->User->find('list', array('fields' => array('User.id', 'User.username', 'Group.group_type'), 'conditions' => array('User.id !=' => $this->Auth->user('id')),
            'recursive' => 0));

        $this->set(compact('users'));
        $this->set('cid', $cid);
    }

    function backstage_reply($id=null) {
        $this->layout = 'admin';

        if (!empty($this->data)) {

            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $mid = $this->Message->getLastInsertID();

                /////////for sent item//////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['to_id'] = $this->data['to_id'];
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 0;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = $this->data['reply'];
                $this->MessageTrack->save($this->data);
                ///////////for inbox///////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['to_id'] = $this->data['to_id'];
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 1;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = $this->data['reply'];
                $this->MessageTrack->save($this->data);
                $this->Session->setFlash(__('The message has been sent successfully.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }
        $msginfo = $this->MessageTrack->findById($id);
        $this->set('msginfo', $msginfo);
    }

    function backstage_group_message() {
        $this->layout = 'admin';

        if (!empty($this->data)) {
            if ($this->data['to_group'] == 'All') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'))));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id')), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Q-Admins') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Q-Admins')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Q-Admins'), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Sellers') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Sellers')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Sellers'), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Buyers') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Buyers')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Buyers'), 'fields' => array('User.id'), 'recursive' => 0));
            }


            if (!empty($totalusers)) {
                $this->Message->create();
                if ($this->Message->save($this->data)) {
                    $mid = $this->Message->getLastInsertID();
                    /////////for sent item//////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = 0;
                    $this->data['MessageTrack']['to_group'] = $this->data['to_group'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 0;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    ///////////for inbox//start for loop from here///////
                    foreach ($users as $user) {
                        $this->data['MessageTrack']['id'] = null;
                        $this->data['MessageTrack']['message_id'] = $mid;
                        $this->data['MessageTrack']['to_id'] = $user['User']['id'];
                        $this->data['MessageTrack']['to_group'] = '';
                        $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                        $this->data['MessageTrack']['msg_type'] = 1;
                        $this->data['MessageTrack']['read'] = 0;
                        $this->data['MessageTrack']['star'] = 0;
                        $this->data['MessageTrack']['reply'] = $this->data['reply'];
                        $this->MessageTrack->save($this->data);
                    }
                    ///////////////end forloop/////////
                    $this->Session->setFlash(__('The message has been sent successfully.', true));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
                }
            } else {
                $this->Session->setFlash(__('The message did not send successfully.', true));
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Message->save($this->data)) {
                $this->Session->setFlash(__('The message has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Message->read(null, $id);
        }
    }

    function delete($id = null) {
        $this->layout = false;
        $this->autoRender = FALSE;
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for message', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->MessageTrack->delete($id)) {
            echo 'Message deleted';
        }
    }

    function backstage_index() {
        $this->layout = 'admin';
        $msginfo = $this->User->find('first', array('conditions' =>
            array('User.id' => $this->Auth->User('id')), 'fields' => array('User.msg_archive', 'User.msg_delete'), 'recursive' => 0));
        if ($msginfo['User']['msg_archive'] == 90) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-90 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-90 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->id = $msgvalue['MessageTrack']['id'];
                $this->MessageTrack->saveField('msg_type', 2);
            }
        } elseif ($msginfo['User']['msg_archive'] == 1) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-365 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'MessageTrack.msg_type !=' => 2, 'Message.created <=' => date('Y-m-d', strtotime('-365 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->id = $msgvalue['MessageTrack']['id'];
                $this->MessageTrack->saveField('msg_type', 2);
            }
        }

        if ($msginfo['User']['msg_delete'] == 90) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-90 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-90 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->delete($msgvalue['MessageTrack']['id']);
            }
        } elseif ($msginfo['User']['msg_delete'] == 1) {
            $msg = $this->MessageTrack->find('all', array('conditions' =>
                array('OR' => array(array('MessageTrack.to_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-365 days'))),
                        array('MessageTrack.from_id' => $this->Auth->User('id'), 'Message.created <=' => date('Y-m-d', strtotime('-365 days')))
                )), 'fields' => array('MessageTrack.id'), 'recursive' => 0));
            foreach ($msg as $msgvalue) {
                $this->MessageTrack->delete($msgvalue['MessageTrack']['id']);
            }
        }
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function backstage_sent() {
        $this->layout = 'admin';
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function backstage_archive() {
        $this->layout = 'admin';
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('OR' => array(array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2), array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2))), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('message', $this->Message->read(null, $id));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $this->Session->setFlash(__('The message has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Message->save($this->data)) {
                $this->Session->setFlash(__('The message has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Message->read(null, $id);
        }
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for message', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Message->delete($id)) {
            $this->Session->setFlash(__('Message deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Message was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function messagebyitem($pid=null, $flag=null) {
        $this->layout = FALSE;
        $this->loadModel('Product');
        //$this->Product->recursive = 1;
        $this->Product->Behaviors->attach('Containable');
        $this->Product->contain('User.username');
        $productinfo = $this->Product->findById($pid, array('Product.id', 'Product.products_title'));
        $this->MessageTrack->Behaviors->attach('Containable');
        $this->MessageTrack->contain('Message.created', 'Message.message', 'Message.item', 'Recipient.username', 'Sender.username');
        $allmsgs = $this->MessageTrack->find('all', array('fields' => array('MessageTrack.id'), 'conditions' => array('Message.item' => $pid)));
        $this->set('allmsgs', $allmsgs);
        $this->set('flag', $flag);
        $this->set('productinfo', $productinfo);
    }

    function chkprogress($pid=null, $sid=null) {
        $this->layout = FALSE;
        $this->autoRender = FALSE;
        $this->MessageTrack->Behaviors->attach('Containable');
        $this->MessageTrack->contain('Message.created', 'Message.item', 'Sender.username');
        return $this->MessageTrack->find('all', array('fields' => array('MessageTrack.id'), 'conditions' => array('Message.item' => $pid, 'MessageTrack.to_id' => $sid), 'order' => array('Message.created DESC'), 'limit' => 1));
    }

    function product_message() {

        $this->layout = FALSE;
        $this->autoRender = false;
        $this->Message->create();
        if ($this->Message->save($this->data)) {
            $mid = $this->Message->getLastInsertID();
            //if (!empty($this->data['MessageTrack']['user_id'])) {
            /////////for sent item//////
            $this->data['MessageTrack']['id'] = null;
            $this->data['MessageTrack']['message_id'] = $mid;
            //$this->data['MessageTrack']['to_id']= $this->data['MessageTrack']['user_id'];
            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
            $this->data['MessageTrack']['msg_type'] = 0;
            $this->data['MessageTrack']['read'] = 0;
            $this->data['MessageTrack']['star'] = 0;
            $this->data['MessageTrack']['reply'] = 0;
            $this->MessageTrack->save($this->data);
            ///////////for inbox///////
            $this->data['MessageTrack']['id'] = null;
            $this->data['MessageTrack']['message_id'] = $mid;
            //$this->data['MessageTrack']['to_id']= $this->data['MessageTrack']['user_id'];
            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
            $this->data['MessageTrack']['msg_type'] = 1;
            $this->data['MessageTrack']['read'] = 0;
            $this->data['MessageTrack']['star'] = 0;
            $this->data['MessageTrack']['reply'] = 0;
            $this->MessageTrack->save($this->data);
        }
        $this->render('successpmsg');
    }

    function setting() {
        $this->layout = 'innerhomeemail';
        $users = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->User('id')), 'fields' => array('User.msg_archive', 'User.msg_delete'), 'recursive' => 0));
        $this->set('users', $users);
    }

    function msg_search($serchmsg=null, $type=null) {
        $this->layout = 'innerhomeemail';
        $this->MessageTrack->recursive = 0;
        if (isset($_POST['serchmsg'])) {
            $serchmsg = $_POST['serchmsg'];
        }
        $this->set('serchmsg', $serchmsg);

        if (isset($this->data['type'])) {
            $type = $this->data['type'];
        }
        $this->set('type', $type);
        $userid = $this->Auth->User('id');
        if ($type == 'inbox') {
            //$this->MessageTrack->recursive = 0;
            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array('OR' => array(array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1, 'Message.subject like' => '%' . $serchmsg . '%'), array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 1, 'Message.message like' => '%' . $serchmsg . '%'))), 'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('index');
        } elseif ($type == 'sent') {

            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array('OR' => array(array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0, 'Message.subject like' => '%' . $serchmsg . '%'), array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0, 'Message.message like' => '%' . $serchmsg . '%'))), 'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('sent');
        } elseif ($type == 'archive') {

            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array(
                        'OR' => array(
                            array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.message like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2, 'Message.message like' => '%' . $serchmsg . '%')
                    )),
                    'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
            $this->render('archive');
        } else {

            $this->paginate = array(
                'MessageTrack' => array('limit' => 100, 'conditions' => array('OR' => array(
                            array('MessageTrack.from_id' => $userid, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'Message.subject like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.to_id' => $userid, 'Message.message like' => '%' . $serchmsg . '%'),
                            array('MessageTrack.from_id' => $userid, 'Message.message like' => '%' . $serchmsg . '%'))),
                    'order' => array('Message.created' => 'desc'
                )));
            $this->set('messages', $this->paginate());
        }
    }

    function new_message($cid=null) {
        $this->layout = 'innerhomeemail';
        if (!empty($this->data)) {
            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $mid = $this->Message->getLastInsertID();
                if (!empty($this->data['MessageTrack']['user_id'])) {
                    /////////for sent item//////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = $this->data['MessageTrack']['user_id'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 0;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    ///////////for inbox///////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = $this->data['MessageTrack']['user_id'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 1;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    /////////////for send email ///////////
                    /* $this->Email->smtpOptions = array(
                      'port'=>'465',
                      'timeout'=>'30',
                      'host' => 'ssl://smtp.gmail.com',
                      'username'=>'kodegeek10@gmail.com',
                      'password'=>'123456aa',
                      );
                      $this->Email->delivery = 'smtp';
                      $this->User->recursive = -1;
                      $to_email = $this->User->read('email', $this->data['MessageTrack']['user_id']);
                      $from_email = $this->User->read('email', $this->Auth->User('id'));
                      $this->Email->to = $to_email['User']['email'];
                      $this->Email->subject = $this->data['Message']['subject'];
                      //$this->Email->replyTo = "<noreply@trakdat.com>";
                      $this->Email->from = $from_email['User']['email'];
                      $this->Email->sendAs = 'html';
                      $this->Email->template = "newmessage";
                      $this->set('mailbody',$this->data['Message']['message']);
                      //$this->set('server_name', $_SERVER['SERVER_NAME']);
                      $this->Email->send();
                      $this->Email->reset(); */
                }

                if (!empty($this->data['to_id'])) {
                    $toid = trim($this->data['to_id']);
                    $this->User->recursive = -1;
                    $uinfo = $this->User->findByUsername($toid);
                    if (!empty($uinfo)) {
                        $toid = $uinfo['User']['id'];
                        if ($toid != $this->data['MessageTrack']['user_id']) {
                            /////////for sent item//////
                            $this->data['MessageTrack']['id'] = null;
                            $this->data['MessageTrack']['message_id'] = $mid;
                            $this->data['MessageTrack']['to_id'] = $toid;
                            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                            $this->data['MessageTrack']['msg_type'] = 0;
                            $this->data['MessageTrack']['read'] = 0;
                            $this->data['MessageTrack']['star'] = 0;
                            $this->data['MessageTrack']['reply'] = $this->data['reply'];
                            $this->MessageTrack->save($this->data);
                            ///////////for inbox///////
                            $this->data['MessageTrack']['id'] = null;
                            $this->data['MessageTrack']['message_id'] = $mid;
                            $this->data['MessageTrack']['to_id'] = $toid;
                            $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                            $this->data['MessageTrack']['msg_type'] = 1;
                            $this->data['MessageTrack']['read'] = 0;
                            $this->data['MessageTrack']['star'] = 0;
                            $this->data['MessageTrack']['reply'] = $this->data['reply'];
                            $this->MessageTrack->save($this->data);
                            ////////////send mail//////////
                            /* $this->Email->smtpOptions = array(
                              'port'=>'465',
                              'timeout'=>'30',
                              'host' => 'ssl://smtp.gmail.com',
                              'username'=>'kodegeek10@gmail.com',
                              'password'=>'123456aa',
                              );
                              $this->Email->delivery = 'smtp';
                              $this->User->recursive = -1;
                              $to_email = $this->User->read('email', $toid);
                              $from_email = $this->User->read('email', $this->Auth->User('id'));
                              $this->Email->to = $to_email['User']['email'];
                              $this->Email->subject = $this->data['Message']['subject'];
                              //$this->Email->replyTo = "<noreply@trakdat.com>";
                              $this->Email->from = $from_email['User']['email'];
                              $this->Email->sendAs = 'html';
                              $this->Email->template = "newmessage";
                              $this->set('mailbody',$this->data['Message']['message']);
                              //$this->set('server_name', $_SERVER['SERVER_NAME']);
                              $this->Email->send();
                             */
                        }
                    }
                }
                $this->Session->setFlash(__('The message has been sent successfully.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }

        $users = $this->Group->User->find('list', array('fields' => array('User.id', 'User.username', 'Group.group_type'), 'conditions' => array('User.id !=' => $this->Auth->user('id')),
            'recursive' => 0));

        $this->set(compact('users'));
        $this->set('cid', $cid);
    }

    function reply($id=null) {
        $this->layout = 'innerhomeemail';
        if (!empty($this->data)) {
            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $mid = $this->Message->getLastInsertID();
                /////////for sent item//////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['to_id'] = $this->data['to_id'];
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 0;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = $this->data['reply'];
                $this->MessageTrack->save($this->data);
                ///////////for inbox///////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['to_id'] = $this->data['to_id'];
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 1;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = $this->data['reply'];
                $this->MessageTrack->save($this->data);

                $this->Session->setFlash(__('The message has been sent successfully.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
            }
        }
        $msginfo = $this->MessageTrack->findById($id);
        $this->set('msginfo', $msginfo);
    }

    function group_message() {
        $this->layout = 'innerhomeemail';

        if (!empty($this->data)) {
            if ($this->data['to_group'] == 'All') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'))));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id')), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Q-Admins') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Q-Admins')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Q-Admins'), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Sellers') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Sellers')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Sellers'), 'fields' => array('User.id'), 'recursive' => 0));
            }
            if ($this->data['to_group'] == 'Buyers') {
                $totalusers = $this->User->find('count', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Buyers')));
                $users = $this->User->find('all', array('conditions' => array('User.active' => 1, 'User.id !=' => $this->Auth->User('id'), 'Group.group_type' => 'Buyers'), 'fields' => array('User.id'), 'recursive' => 0));
            }


            if (!empty($totalusers)) {
                $this->Message->create();
                if ($this->Message->save($this->data)) {
                    $mid = $this->Message->getLastInsertID();
                    /////////for sent item//////
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['message_id'] = $mid;
                    $this->data['MessageTrack']['to_id'] = 0;
                    $this->data['MessageTrack']['to_group'] = $this->data['to_group'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = 0;
                    $this->data['MessageTrack']['read'] = 0;
                    $this->data['MessageTrack']['star'] = 0;
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];
                    $this->MessageTrack->save($this->data);
                    ///////////for inbox//start for loop from here///////
                    foreach ($users as $user) {
                        $this->data['MessageTrack']['id'] = null;
                        $this->data['MessageTrack']['message_id'] = $mid;
                        $this->data['MessageTrack']['to_id'] = $user['User']['id'];
                        $this->data['MessageTrack']['to_group'] = '';
                        $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                        $this->data['MessageTrack']['msg_type'] = 1;
                        $this->data['MessageTrack']['read'] = 0;
                        $this->data['MessageTrack']['star'] = 0;
                        $this->data['MessageTrack']['reply'] = $this->data['reply'];
                        $this->MessageTrack->save($this->data);
                    }
                    ///////////////end forloop/////////
                    $this->Session->setFlash(__('The message has been sent successfully.', true));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The message could not be saved. Please, try again.', true));
                }
            } else {
                $this->Session->setFlash(__('The message did not send successfully.', true));
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    function viewfront($id = null) {
        $this->layout = FALSE;
        if (!$id) {
            $this->Session->setFlash(__('Invalid message', true));
            $this->redirect(array('action' => 'index'));
        }
        $msgid = $this->MessageTrack->findById($id);
        $this->MessageTrack->id = $id;
        $this->MessageTrack->saveField('read', 1);
        $this->set('message', $msgid);
    }

    function sent() {
        $this->layout = 'innerhomeemail';
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 0), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function archive() {
        $this->layout = 'innerhomeemail';
        $this->MessageTrack->recursive = 0;
        $userid = $this->Auth->User('id');
        $this->paginate = array(
            'MessageTrack' => array('limit' => 50, 'conditions' => array('OR' => array(array('MessageTrack.from_id' => $userid, 'MessageTrack.msg_type' => 2), array('MessageTrack.to_id' => $userid, 'MessageTrack.msg_type' => 2))), 'order' => array('Message.created' => 'desc'
            )));
        $this->set('messages', $this->paginate());
    }

    function newsletter() {
        $this->layout = 'innerhomeemail';
        if ($this->Session->read('grouptype') != 'Sellers') {
            $this->redirect(array('action' => 'index'));
        }

        $this->loadModel('Subscribe');
        $userIds = $this->Subscribe->find('all', array('conditions' => array('Subscribe.seller_id' => $this->Auth->User('id'), 'Subscribe.subscription' => '1'), 'fields' => array('Subscribe.buyer_id')));
        if (count($userIds) == 0) {
            $this->render('nouser');
            return;
        }

        if ($this->data) {

            foreach ($userIds as $userId) {
                if ($this->Message->save($this->data)) {
                    $this->data['MessageTrack']['message_id'] = $this->Message->getLastInsertID();
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['to_id'] = $userId['Subscribe']['buyer_id'];
                    $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                    $this->data['MessageTrack']['msg_type'] = '1';
                    $this->data['MessageTrack']['to_group'] = 'Subscribed';
                    $this->data['MessageTrack']['reply'] = $this->data['reply'];

                    $this->MessageTrack->save($this->data);

                    //for sent item
                    $this->data['MessageTrack']['id'] = null;
                    $this->data['MessageTrack']['msg_type'] = '0';

                    $this->MessageTrack->save($this->data);

                    $check = TRUE;
                }
            }

            if ($check) {
                $this->Session->setFlash(__('The messages has been sent successfully.', true));
            } else {
                $this->Session->setFlash(__('The messages not sent.', true));
            }

            $this->redirect(array('action' => 'index'));
        }
    }

    function dispute_message($disid=null, $flag=null) {
        $this->layout = FALSE;
        $this->loadModel('Dispute');
        if ($flag == 1) {
            $this->MessageTrack->Behaviors->attach('Containable');
            $this->MessageTrack->contain('Message.created', 'Message.message', 'Message.item', 'Recipient.username', 'Sender.username');
            $allmsgs = $this->MessageTrack->find('all', array('fields' => array('MessageTrack.id'), 'conditions' => array('MessageTrack.case_id' => $disid, 'MessageTrack.msg_type' => 1)));
            $this->set('allmsgs', $allmsgs);
            $this->render('historymsg');
        }
        if (!empty($this->data)) {
            $this->Dispute->id = $this->data['MessageTrack']['case_id'];
            $this->Dispute->saveField('status', 'Pending');
            $this->Message->create();
            if ($this->Message->save($this->data)) {
                $mid = $this->Message->getLastInsertID();
                /////////for sent item//////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 0;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = 0;
                $this->MessageTrack->save($this->data);
                ///////////for inbox///////
                $this->data['MessageTrack']['id'] = null;
                $this->data['MessageTrack']['message_id'] = $mid;
                $this->data['MessageTrack']['from_id'] = $this->Auth->User('id');
                $this->data['MessageTrack']['msg_type'] = 1;
                $this->data['MessageTrack']['read'] = 0;
                $this->data['MessageTrack']['star'] = 0;
                $this->data['MessageTrack']['reply'] = 0;
                $this->MessageTrack->save($this->data);
                $this->render('successpmsg');
            }
        } else {
            $this->Dispute->Behaviors->attach('Containable');
            if ($flag == 2) {
                $this->Dispute->contain('Seller.username');
            } else {
                $this->Dispute->contain('Disputer.username');
            }
            $disputeinfo = $this->Dispute->findById($disid);
            $this->set('flag', $flag);
            $this->set('disputeinfo', $disputeinfo);
        }
    }

}

?>